package com.tywj.lib.social.share

import com.alipay.share.sdk.openapi.APImageObject
import com.alipay.share.sdk.openapi.APMediaMessage
import com.alipay.share.sdk.openapi.APWebPageObject
import com.alipay.share.sdk.openapi.SendMessageToZFB
import com.tywj.lib.core.common.util.ToastUtil
import com.tywj.lib.social.R

import com.tywj.lib.social.SocialUtil
import com.tywj.lib.social.entity.ImageShareEntity
import com.tywj.lib.social.entity.WebPageShareEntity
import com.tywj.lib.social.interfaces.Callback
import com.tywj.lib.social.interfaces.IShare

/**
 * <pre>
 *     author : June Yang
 *     time   : 2020/5/16
 *     desc   : 支付宝分享
 *     version: 1.0.0
 * </pre>
 */
class AlipayShare : IShare {

    override fun shareWebPage(entity: WebPageShareEntity, callback: Callback?) {
        // 判断用户是否安装支付宝客户端
        if (!SocialUtil.alipayApi.isZFBAppInstalled) {
            ToastUtil.shortToast(R.string.social_not_install_alipay)
            return
        }

        // 判断用户安装的支付宝是否支持分享
        if (!SocialUtil.alipayApi.isZFBSupportAPI) {
            ToastUtil.shortToast(R.string.social_not_support_alipay_share)
            return
        }

        SocialUtil.share.callback = callback

        val webPageObject = APWebPageObject()
        webPageObject.webpageUrl = entity.url

        val webMessage = APMediaMessage()
        webMessage.title = entity.title
        webMessage.description = entity.content
        webMessage.mediaObject = webPageObject
        webMessage.thumbUrl = entity.imgUrl

        // 将分享消息对象包装成请求对象
        val req = SendMessageToZFB.Req()
        req.message = webMessage

        // 发送请求
        SocialUtil.alipayApi.sendReq(req)
    }
    //待测试
    override fun shareImage(entity: ImageShareEntity, callback: Callback?) {
        // 判断用户是否安装支付宝客户端
        if (!SocialUtil.alipayApi.isZFBAppInstalled) {
            ToastUtil.shortToast(R.string.social_not_install_alipay)
            return
        }

        // 判断用户安装的支付宝是否支持分享
        if (!SocialUtil.alipayApi.isZFBSupportAPI) {
            ToastUtil.shortToast(R.string.social_not_support_alipay_share)
            return
        }

        SocialUtil.share.callback = callback

        val imageObject = APImageObject()
        imageObject.imagePath = entity.imageUrl

        val webMessage = APMediaMessage()
        webMessage.mediaObject = imageObject
        webMessage.thumbUrl = entity.imageUrl

        // 将分享消息对象包装成请求对象
        val req = SendMessageToZFB.Req()
        req.message = webMessage

        // 发送请求
        SocialUtil.alipayApi.sendReq(req)
    }
}